package solved;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/*
 ID:	liluyan1
 LANG:	JAVA
 TASK:	dualpal
 */
public class dualpal {
	public static void main(String[] args) throws IOException {
		BufferedReader f = new BufferedReader(new FileReader("dualpal.in"));
		String[] line = f.readLine().split(" ");
		int N = Integer.parseInt(line[0]);
		int S = Integer.parseInt(line[1]);
		
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"dualpal.out")));
		for (int i=S+1; i<Integer.MAX_VALUE; i++) {
			int count = 0;
			for (int b=2; b<=10; b++) {
				if (isPalindrome(Integer.toString(i, b))) {
					count++;
					if (count == 2) {
						out.println(i);
						N--;
						break;
					}
				}
			}
			if (N == 0) break;
		}
		out.close();
		System.exit(0);
	}
	
	public static boolean isPalindrome(String s) {
		int N = s.length();
		for (int i=0; i<s.length()/2; i++) {
			if (s.charAt(i) != s.charAt(N-i-1)) return false;
		}
		return true;
	}
}
